//
//  ZOEPickerView.m
//  BasicModule
//
//  Created by 徐秀滨 on 2018/3/16.
//  Copyright © 2018年 Xiamen Zoenet Tech Co.,Ltd. All rights reserved.
//

#import "ZOEPickerView.h"
#import "ZOEPickerHeaderView.h"

#define ZOEDatePickerViewRowHeight 35 //每一行的高度
#define DefaultTextColor [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1] //日期默认颜色
#define SelectedTextColor [UIColor colorWithRed:34/255.0 green:34/255.0 blue:34/255.0 alpha:1] //日期选中颜色

@interface ZOEPickerView()<UIPickerViewDelegate,UIPickerViewDataSource,ZOEPickerHeaderViewDelegate>

@property (nonatomic, strong) ZOEPickerHeaderView *headerView;
@property (nonatomic, strong) UIView *bgView;

//只用在数据集内部控制
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *selectedTitles;

@end

@implementation ZOEPickerView

- (instancetype)init {
    self = [super init];
    if(self){
        [self setUI];
    }
    return self;
}

- (instancetype)initWithDataSource:(NSMutableArray*)dataSource selectedIndexes:(NSMutableArray*)selectedIndexes {
    self = [super init];
    if(self){
        NSAssert([dataSource isKindOfClass:[NSArray class]] && dataSource.count > 0, @"'dataSource' error：无效的数据集");
        NSAssert(selectedIndexes == nil || selectedIndexes.count == dataSource.count, @"'dataSource or selectedIndexes error'：选中索引必须与数据集对应得上");
        [self setUI];
        [self resetDataSource:dataSource selectedIndexes:selectedIndexes];
    }
    return self;
}

- (instancetype)initWithDataSource:(NSMutableArray*)dataSource selectedIndex:(NSInteger)selectedIndex {
    self = [super init];
    if(self){
        NSAssert([dataSource isKindOfClass:[NSArray class]] && dataSource.count > 0, @"'dataSource' error：无效的数据集");
        NSMutableArray *temp = [[NSMutableArray alloc] init];
        [temp addObject:dataSource];
        _dataSource = temp;
        
        [self setUI];
        [self resetDataSource:dataSource selectedIndex:selectedIndex];
    }
    return self;
}

- (void)setUI {
    self.backgroundColor = [UIColor whiteColor];
    self.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 216, [[UIScreen mainScreen] bounds].size.width, 216);
    [self addSubview:self.headerView];
    [self addSubview:self.pickerView];
    self.pickerView.frame = CGRectMake(10, CGRectGetMaxY(self.headerView.frame), self.frame.size.width - 20, self.frame.size.height - self.headerView.frame.size.height);
}

#pragma mark - Delegate

#pragma mark -UIPickerView

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return self.dataSource.count;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [self.dataSource[component] count];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    UILabel *dateLabel = (UILabel *)view;
    if (!dateLabel) {
        dateLabel = [[UILabel alloc] init];
        [dateLabel setFont:[UIFont systemFontOfSize:18]];
        [dateLabel setTextColor:DefaultTextColor];
        [dateLabel setBackgroundColor:[UIColor clearColor]];
        dateLabel.textAlignment = NSTextAlignmentCenter;
    }
    dateLabel.text = self.dataSource[component][row];
    if([self.selectedIndexes[component] integerValue] == row){
        [dateLabel setTextColor:SelectedTextColor];
    }else{
        [dateLabel setTextColor:DefaultTextColor];
    }
    return dateLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [pickerView reloadComponent:component];
    self.selectedTitles[component] = self.dataSource[component][row];
    self.selectedIndexes[component] = @(row);
    [pickerView reloadComponent:component];
}

#pragma mark -ZOEPickerHeaderViewDelegate

- (void)cancel {
    [self.bgView removeFromSuperview];
    [self removeFromSuperview];
}

- (void)confirm {
    [self.bgView removeFromSuperview];
    [self removeFromSuperview];
    if([self.delegate respondsToSelector:@selector(zoePickerView:confirmWithIndexes:titles:)]){
        [self.delegate zoePickerView:self confirmWithIndexes:self.selectedIndexes titles:self.selectedTitles];
    }
}

#pragma mark - Event

- (void)setSelectedIndexes:(NSMutableArray *)selectedIndexes {
    if(!self.dataSource){
        return;
    }
    NSAssert(selectedIndexes.count == self.dataSource.count, @"'selectedIndexes error'：选中索引必须与数据集对应得上");
    _selectedIndexes = selectedIndexes;
    [_selectedTitles removeAllObjects];
    for(NSInteger i = 0; i < selectedIndexes.count; i++){
        NSInteger index = [selectedIndexes[i] integerValue];
        [_selectedTitles addObject:self.dataSource[i][index]];
    }
    for(NSInteger component = 0 ; component <  self.dataSource.count; component ++){
        [self.pickerView selectRow:[self.selectedIndexes[component] integerValue] inComponent:component animated:NO];
    }
    [self.pickerView reloadAllComponents];
}

- (void)setSelectedIndex:(NSInteger)selectedIndex {
    if(!self.dataSource){
        return;
    }
    _selectedIndexes = [[NSMutableArray alloc] initWithObjects:@(selectedIndex), nil];
    [self setSelectedIndexes:_selectedIndexes];
}

- (void)resetDataSource:(NSMutableArray*)dataSource selectedIndex:(NSInteger)selectedIndex {
    NSMutableArray *temp = [[NSMutableArray alloc] init];
    [temp addObject:dataSource];
    NSMutableArray *selectedIndexes = [NSMutableArray new];
    if(selectedIndex >= 0){
        [selectedIndexes addObject:@(selectedIndex)];
    }
    [self resetDataSource:temp selectedIndexes:selectedIndexes];
}

- (void)resetDataSource:(NSMutableArray*)dataSource selectedIndexes:(NSMutableArray*)selectedIndexes {
    NSMutableArray *temp = [[NSMutableArray alloc] init];
    if([[dataSource firstObject] isKindOfClass:[NSArray class]]){
        temp = dataSource;
    }else{
        [temp addObject:dataSource];
    }
    _dataSource = temp;
    //默认选中
    if(selectedIndexes.count > 0){
        _selectedIndexes = [selectedIndexes mutableCopy];
        _selectedTitles = [[NSMutableArray alloc] initWithCapacity:_dataSource.count];
        for(NSInteger i = 0; i < selectedIndexes.count; i++){
            NSInteger index = [selectedIndexes[i] integerValue];
            [_selectedTitles addObject:_dataSource[i][index]];
        }
    }else{
        //默认选中中间的值
        _selectedIndexes = [[NSMutableArray alloc] initWithCapacity:_dataSource.count];
        _selectedTitles = [[NSMutableArray alloc] initWithCapacity:_dataSource.count];
        for(NSArray *array in _dataSource){
            NSInteger index = array.count / 2;
            [_selectedIndexes addObject:@(index)];
            [_selectedTitles addObject:array[index]];
        }
    }
    for(NSInteger component = 0 ; component <  self.dataSource.count; component ++){
        [self.pickerView selectRow:[self.selectedIndexes[component] integerValue] inComponent:component animated:YES];
    }
}

#pragma mark - action

- (void)show {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self.bgView];
    [keyWindow addSubview:self];
}

- (void)hide {
    [self.bgView removeFromSuperview];
    [self removeFromSuperview];
}

#pragma mark - Property

- (ZOEPickerHeaderView*)headerView {
    if(!_headerView){
        _headerView = [[ZOEPickerHeaderView alloc] init];
        _headerView.delegate = self;
    }
    return _headerView;
}

- (UIView *)bgView {
    if(!_bgView){
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height - self.frame.size.height)];
        _bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    }
    return _bgView;
}

- (UIPickerView*)pickerView {
    if(!_pickerView){
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    return _pickerView;
}

@end
